using System.Xml.Serialization;

namespace 订单管理系统窗体应用
{



    public partial class MainForm : Form
    {
        List<Order> orders = new List<Order>();
        List<Order> showOrders = new List<Order>();

        public MainForm()
        {
            InitializeComponent();
        }

        private void CreateOrderButton_Click(object sender, EventArgs e)
        {
            CreateForm createForm = new CreateForm(this);
            createForm.ShowDialog();
        }


        private void RemoveOrderButton_Click(object sender, EventArgs e)
        {
            RemoveForm removeForm = new RemoveForm(this);
            removeForm.ShowDialog();
        }

        private void ExportButton_Click(object sender, EventArgs e)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Order>));
            using (FileStream fileStream = new FileStream("OrderList.xml", FileMode.Create))
            {
                xmlSerializer.Serialize(fileStream, this.orders);
            }
            MessageBox.Show("订单导出成功！");
        }

        private void ImportButton_Click(object sender, EventArgs e)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Order>));
            try
            {
                using (FileStream fileStream = new FileStream("OrderList.xml", FileMode.Open))
                {
                    orders = (List<Order>)xmlSerializer.Deserialize(fileStream)!;
                }
                MessageBox.Show("订单导入成功！");
            }
            catch
            {
                MessageBox.Show("订单不存在！");
            }
        }

        private void SearchButton_Click(object sender, EventArgs e)
        {
            SearchForm searchForm = new SearchForm(this.orders);
            searchForm.ShowDialog();
        }

        public void create(Order order)
        {
            orders.Add(order);
        }

        public void remove(int id)
        {
            bool flag = false;
            int i = 0;
            foreach (Order order in orders)
            {
                if (order.Id == id)
                {
                    flag = true;
                    break;
                }
                i++;
            }
            if (flag == true)
            {
                orders.Remove(orders[i]);
                MessageBox.Show("ID为" + id + "的订单已成功删除！");
            }
            else
            {
                MessageBox.Show("ID为" + id + "的订单不存在！");                
            }
        }

        public void show()
        {

        }

        private void ShowButton_Click(object sender, EventArgs e)
        {
            ShowForm showForm = new ShowForm(this.orders);
            showForm.ShowDialog();
        }
    }


    public class OrderDetails
    {
        private string name;
        public string Name { get { return name; } set { name = value; } }
        private int number;
        public int Number { get { return number; } set { number = value; } }
        private double price;
        public double Price { get { return price; } set { price = value; } }

        public OrderDetails()
        {
            this.Name = string.Empty;
            this.Number = 0;
            this.Price = 0;
        }

        public OrderDetails(string name, int number, double price)
        {
            this.Name = name;
            this.Number = number;
            this.Price = price;
        }

        public double getAllPrice()
        {
            return this.price * this.number;
        }

    }

    public class Order : IComparable
    {
        public int Id { get; set; }
        public string Customer { get; set; }
        public string Date { get; set; }
        public double Money { get; set; }

        public List<OrderDetails> orderDetails = new List<OrderDetails>();


        public Order()
        {
            this.Id = 0;
            this.Customer = string.Empty;
            this.Date = string.Empty;
            this.Money = 0;
        }

        public Order(int id, string customer, string date)
        {
            this.Id = id;
            this.Customer = customer;
            this.Date = date;
            this.Money = 0;
        }

        public void setMoney()
        {
            double money = 0;
            foreach (OrderDetails details in orderDetails)
            {
                money += details.getAllPrice();
            }
            this.Money = money;
        }

        public void addOrderDetails(string name, int number, double price)
        {
            OrderDetails details = new OrderDetails(name, number, price);
            this.orderDetails.Add(details);
        }

        public void removeOrderDetails()
        {
            Console.WriteLine("Please intput the id of the orderdetails to remove:");
            int id = Convert.ToInt32(Console.ReadLine());
            this.orderDetails.RemoveAt(id);
            Console.WriteLine("Remove successfully");
        }

        public void showOrderDetails()
        {
            foreach (OrderDetails details in this.orderDetails)
            {
                Console.WriteLine("Id Name Number Price");
                Console.WriteLine("{0} {1} {2} {3}",
                    this.orderDetails.IndexOf(details), details.Name, details.Number, details.Price);
            }
        }

        #region IComparable Members
        public int CompareTo(object? obj)
        {
            if (obj != null)
            {
                Order? order = obj as Order;
                return this.Id.CompareTo(order?.Id);
            }
            else
                return -1;
        }

        public override bool Equals(object? obj)
        {
            Order? order = (obj as Order);
            return this.Id.Equals(order?.Id);
        }

        public override int GetHashCode()
        {
            return Convert.ToInt32(this.Id);
        }

        public override string ToString()
        {
            string str = this.Id.ToString() + " " +
                         this.Customer.ToString() + " " +
                         this.Date.ToString() + " " +
                         this.Money.ToString();
            return str;
        }
    }
}
#endregion